package com.skytech.tax.customsdatawarehouse.singlewindow.customsagententrusted.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.customsagententrusted.domain.entity.EntrustedAgreementFile;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author Lianhong
 * @since 2022/6/15
 */
@Repository
public class EntrustedAgreementFileDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String existsCustomsDeclarationFileSql = "SELECT count(1) FROM sw_entrusted_agreement_file WHERE seqNo = ?";

    public boolean exists(String seqNo) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsCustomsDeclarationFileSql, new Object[]{seqNo}, Integer.class);
        return count != null && count > 0;
    }

    private String insertCustomsDeclarationFileSql = "INSERT INTO sw_entrusted_agreement_file (seqNo, file, storageTime) VALUES (:seqNo, :file, :storageTime)";

    public void save(EntrustedAgreementFile entrustedAgreementFile) {
        entrustedAgreementFile.setStorageTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        this.namedParameterJdbcTemplate.update(this.insertCustomsDeclarationFileSql, new BeanPropertySqlParameterSource(entrustedAgreementFile));
    }

    private String selectEntrustedAgreementFileSql = "SELECT file FROM sw_entrusted_agreement_file WHERE seqNo = ?";

    public String findBySeqNo(String seqNo) {
        return namedParameterJdbcTemplate.getJdbcOperations().queryForObject(selectEntrustedAgreementFileSql,
                new Object[]{seqNo}, String.class);
    }

}
